<template>
  <!-- 场站发送通知给车队 2018/3/7 11:44 zhangQ -->
  <div>
    <Row :style="{minHeight:350+'px'}">
      <i-col span="20" offset="4">
        <Form ref="sendNoticeForm" :model="sendNoticeForm" label-position="left" :label-width="100">
          <FormItem label="对位股道" prop="paraPositionTrack">
            <Select v-model="sendNoticeForm.paraPositionTrack" style="width: 200px">
              <template v-for="item in trackOptions">
                <Option :value="item" :key="item">{{item}}</Option>
              </template>
            </Select>
          </FormItem>
          <FormItem label="预计到达时间" prop="estimateArriveTime">
            <DatePicker type="datetime" placeholder="选择需求时间" v-model="sendNoticeForm.estimateArriveTime" style="width: 200px"></DatePicker>
          </FormItem>
          <template v-for="(item, index) in trackName">
            <FormItem :label="item.login_name">
              <Select v-model="sendNoticeForm.data[index].address" filterable multiple :value="sendNoticeForm.data[index].address" style="width: 400px">
                <template v-for="(addressItem, index) in address">
                  <Option :value="addressItem" :key="index">{{addressItem}}</Option>
                </template>
              </Select>
            </FormItem>
          </template>
        </Form>
      </i-col>
    </Row>
    <div class="xlinkModal-footer">
      <Button type="primary" @click="sendNoticeToFleet">确认发送</Button>
    </div>
  </div>
</template>

<script>
  import {getTrackList, getOwnFleet, getAddressList, sendNotice} from '@api/backEnd/station'
  import moment from 'moment'

  export default {
    name: 'send-notice',
    data () {
      let sendNoticeForm = {
        paraPositionTrack: '',
        estimateArriveTime: '',
        mainOrderNo: '',
        data: []
      }
      let trackOptions = []
      let trackName = []
      let address = []
      let datas = {sendNoticeForm, trackOptions, trackName, address}
      return datas
    },
    props: {
      currentMainOrder: {
        type: Object,
        default () {
          return {}
        }
      }
    },
    computed: {},
    methods: {
      getData () {
        getTrackList().then(res => {
          if (res.data.isSuccess === 1) {
            this.trackOptions = res.data.data
          }
        })
        getOwnFleet().then(res => {
          if (res.data.isSuccess === 1) {
            this.trackName = res.data.data
            this.newObj = this.trackName
            for (let i = 0; i < this.newObj.length; i++) {
              this.sendNoticeForm.data.push({
                truckName: this.newObj[i].login_name,
                address: []
              })
            }
          }
        })
        getAddressList().then(res => {
          if (res.data.isSuccess === 1) {
            this.address = res.data.data
          }
        })
      },
      /**
       * TODO(发送通知给车队)
       * @auther zhangQ
       * @date 2018/3/16 17:26
       */
      sendNoticeToFleet () {
        let val = this.sendNoticeForm.data
        let sendData = []
        for (let item in val) {
          if (+val[item].address.length !== 0) {
            sendData.push({
              address: val[item].address,
              truckName: val[item].truckName
            })
          }
        }
        this.sendNoticeForm.estimateArriveTime = moment(this.sendNoticeForm.estimateArriveTime).format('YYYY-MM-DD HH:mm:ss')
        const paraPositionTrack = this.sendNoticeForm.paraPositionTrack
        const mainOrderNo = this.currentMainOrder.orderNo
        const estimateArriveTime = this.sendNoticeForm.estimateArriveTime
        // if (estimateArriveTime < moment().format()) {
        //   this.$Message.error('预计到达时间不得小于当前时间')
        //   return false
        // }
        const data = JSON.stringify(sendData)
        const param = {paraPositionTrack, mainOrderNo, estimateArriveTime, data}
        sendNotice(param).then(res => {
          if (res.data.isSuccess === 1) {
            this.$Message.info('发送成功')
            this.$emit('close')
          } else {
            this.$Message.error(res.data.res)
            console.log(res.data.res)
          }
        })
      }
    },
    mounted () {
      this.getData()
    },
    components: {}
  }
</script>

<style scoped>

</style>
